package com.jgsu.farm.controller;

import com.jgsu.farm.entity.Plant;
import com.jgsu.farm.exception.*;
import com.jgsu.farm.service.PlantService;
import com.jgsu.farm.utils.CommonResult;
import com.jgsu.farm.utils.ResultCode;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

@Slf4j
@Api(value = "farm/sysuser", tags = "作物信息管理模块")  //描述信息用的，标注在类上
@RestController
@RequestMapping("farm/sysuser/crop")
public class PlantController {

    @Autowired
    private PlantService plantService;


    /**
     * 作物增加
     * plant类中的category属性如果插入不成功，则要先去category插入值，
     * 再通过返回对应的ID进行插入语
     * plant类的plantImage属性，通过Image表对应得id进行插入
     */
    @ApiOperation(value = "作物信息添加功能")
    @PostMapping(value = "/add", produces = "application/json")
    public CommonResult corpAdd(Plant plant) {
        try {
            plantService.addPlant(plant);
            log.info("{}植物添加成功", plant.getChinesename());
            return CommonResult.success(ResultCode.SUCCESS);
        } catch (DataAddException e) {
            log.info("{}植物添加失败", plant.getChinesename());
            throw new DataAddException(e.getCode(), e.getMessage());
        }
    }

    @ApiOperation(value = "作物信息删除功能", notes = "删除单条数据")
    @DeleteMapping(value = "/delete", produces = "application/json")
    public CommonResult cropDelete(@RequestParam("plantid") Integer plantid) {
        try {
            plantService.deletePlant(plantid);
            log.info("删除id为:{}的作物信息", plantid);
            return CommonResult.success(ResultCode.SUCCESS);
        } catch (DataNotExistException e) {
            //controller层抛出的异常，将返回给前端进行报错提示
            log.error("不存在id为：{}的作物信息，删除失败", plantid);
            throw new DataNotExistException(e.getCode(), e.getMessage());
        }
    }


    @ApiOperation(value = "分页查询作物信息", notes = "分页查询作物")
    @GetMapping(produces = "application/json")
    public CommonResult<PageInfo<Plant>> listPlants(@RequestParam(value = "page", required = false, defaultValue = "1") int page,
                                                    @RequestParam(value = "pagesize", required = false, defaultValue = "10") int pagesize) {
        try {
            PageInfo<Plant> user = plantService.getPlantPages(page, pagesize);
            log.info("查询第{}页所有植物信息", page);
            return CommonResult.success(user);
        } catch (DataNotExistException e) {
            log.error("第{}页普植物信息查询失败", page);
            throw new DataNotExistException(e.getCode(), e.getMessage());
        }
    }

    @ApiOperation(value = "根据id查询作物信息", notes = "根据id查询作物")
    @GetMapping(value = "/plantid",produces = "application/json")
    public CommonResult findById(Integer plantid) {
        Plant plant = plantService.selectPlant(plantid);
        return CommonResult.success(plant);
    }

    @ApiOperation(value = "修改作物信息")
    @PutMapping(value = "/update", produces = "application/json")
    public CommonResult updatePlant(Plant plant) {
        try {
            plantService.updatePlant(plant);
            log.info("更新作物信息数据，更新的id为：{}", plant.getPlantid());
            return CommonResult.success(plant);
        } catch (DataNotExistException e) {
            log.error("更新作物的id为:{}的信息失败", plant.getPlantid());
            throw new DataNotExistException(e.getCode(), e.getMessage());
        }

    }

}
